import Vue from 'vue'
import VueRouter from 'vue-router'
import Films from '@/views/Films'
import Cinemas from '@/views/Cinemas'
import Center from '@/views/Center'
import Nowplaying from '@/views/films/Nowplaying'
import Comingsoon from '@/views/films/Comingsoon'
import Search from '@/views/Search'
import City from '@/views/City'
import Detail from '@/views/Detail'
import Order from '@/views/Order'
import Login  from '@/views/Login'


Vue.use(VueRouter) // 注册路由插件

const routes = [
  {
    path:'/films',
    component:Films,
    children: [
      {
        path:'/films/nowplaying',
        component: Nowplaying
      },
      {
        path:'/films/comingsoon',
        component: Comingsoon
      },
      {
        path:'/films',
        redirect:'/films/nowplaying'
      }
    ]
  },
  {
    name:"zyxdetail",
    path:'/detail/:id',
    component:Detail
  },
  {
    path:'/cinemas',
    component:Cinemas
  },
  {
    path:'/cinemas/search',
    component:Search
  },
  {
    path:'/city',
    component:City
  },
  {
    path:'/center',
    component:Center,
    meta:{
      isZyxRequired:true
    }
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/order',
    component:Order,
    meta:{
      isZyxRequired:true
    } 
  },
  //重定向
  {
    path:'/',
    redirect:'/films'
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})

//全局拦截

router.beforeEach((to,from,next) => {
  console.log(to);

if (to.meta.isZyxRequired) {
  if(localStorage.getItem('token')){
    next()
  } else {
    next('/login')
  }
} else {
  next()
}
})

export default router
